﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Server.Data
{
    /// <summary>
    /// The Room object
    /// </summary>

    public class Room
    {
        private Int32 _id;
        private String _name;
        private List<Player> _listPlayer;        
        private Int32 _playerTurn;
        private Boolean _isGameStarted;

        /// <summary>
        /// Create a new room
        /// </summary>
        public Room()
        {
            _listPlayer = new List<Player>();
            _listPlayer.Add(null);
            _listPlayer.Add(null);
            _listPlayer.Add(null);
            _listPlayer.Add(null);
            _playerTurn = -1;
            _isGameStarted = false;
        }

        /// <summary>
        /// Create a new room with a specified name
        /// </summary>
        /// <param name="Name"></param>        
        public Room(String name, Int32 id)
        {
            _name = name;
            _id = id;
            _listPlayer = new List<Player>();
            _listPlayer.Add(null);
            _listPlayer.Add(null);
            _listPlayer.Add(null);
            _listPlayer.Add(null);
            
            _playerTurn = -1;
        }
      
        /// <summary>
        /// Gets or sets the id of the room
        /// </summary>        
        public Int32 ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        /// <summary>
        /// Gets or Sets the name of the room
        /// </summary>
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        /// <summary>
        /// Gets or Sets the player list of the room
        /// </summary>
        public List<Player> ListPlayer
        {
            get
            {
                return _listPlayer;
            }
            set
            {
                _listPlayer = value;
            }
        }
        
        /// <summary>
        /// Gets of sets the turn of the game
        /// </summary>
        public Int32 PlayerTurn
        {
            get
            {
                return _playerTurn;
            }
            set
            {
                _playerTurn = value;
            }
        }

        /// <summary>
        /// Gets or sets the game state
        /// </summary>
        public Boolean IsGameStarted
        {
            get
            {
                return _isGameStarted;
            }

            set
            {
                _isGameStarted = value;
            }
        }

        /// <summary>
        /// Find a empty position in the room
        /// </summary>
        /// <returns>Return the position or -1 if isn't a place left</returns>
        public Int32 GetFirstFreePosition()
        {
            for (int i = 0; i < 4; i++)
                if (_listPlayer[i] == null)
                    return i;
            return -1;
        }

        /// <summary>
        /// Count players from room
        /// </summary>
        /// <returns>Number of player</returns>
        public Int32 GetNumberOfPlayer()
        {
            Player p;
            Int32 _count = 0;
            for (int i = 0; i < 4; i++)
            {
                p = _listPlayer[i];
                if (p != null)
                    _count++;
            }
            return _count;
        }

        /// <summary>
        /// Find the position of a player in the room
        /// </summary>
        /// <param name="nickname">Nickname</param>
        /// <returns>The position or -1 if isn't in room</returns>
        public Int32 GetPlayerPosition(String nickname)
        {
            Player p;
            for (int i = 0; i < 4; i++)
            {
                p = _listPlayer[i];
                if (p != null)
                    if (p.NickName.Equals(nickname))
                        return i;
            }
            return -1;                
        }

        /// <summary>
        /// Remove a player from a room
        /// </summary>
        /// <param name="nickname">Nickname</param>
        public void RemovePlayer(String nickname)
        {
            Player p = _listPlayer[GetPlayerPosition(nickname)];
            p = null;
        }

        /// <summary>
        /// Finds the position of the next turn playey
        /// </summary>
        /// <returns>The position</returns>
        public Int32 FindTurn()
        {
            Player p;
            for (int i = _playerTurn + 1; i < 4; i++)
            {
                p = _listPlayer[i];
                if (p != null)
                {
                    _playerTurn = i;
                    return i;
                }
            }
            _playerTurn = 0;
            return 0;               
        }
    }
}
